package com.coffee.admin.service.platform.impl;

import com.coffee.admin.api.platform.query.PlatformRolePermissionQuery;
import com.coffee.admin.api.platform.result.PlatformRolePermissionResult;
import com.coffee.admin.api.platform.service.PlatformRolePermissionService;
import com.coffee.admin.service.platform.bean.PlatformRolePermission;
import com.coffee.admin.service.platform.dao.PlatformRolePermissionDao;
import com.coffee.core.entity.Page;
import com.coffee.core.entity.PagerInfo;
import com.coffee.core.enums.ExceptionEnum;
import com.coffee.core.exception.BusinessException;
import com.coffee.core.util.BeanUtilExt;
import com.coffee.core.util.DTOUtils;
import com.coffee.core.util.StringUtil;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.Resource;
import java.util.*;


@DubboService(dynamic = true)
public class PlatformRolePermissionServiceImpl implements PlatformRolePermissionService {
	@Resource
	private PlatformRolePermissionDao	platformRolePermissionDao;

	@Override
	public Page<PlatformRolePermissionResult> getPager(PlatformRolePermissionQuery param, PagerInfo pr) {
		Page<PlatformRolePermissionResult> pager=new Page<PlatformRolePermissionResult>(pr.getStart(), pr.getPageSize());
		Integer count=platformRolePermissionDao.count(param);
		pager.setTotalPage(count);
		if(count>0) {
			List<PlatformRolePermission> list=platformRolePermissionDao.page(param, pr.getStart(), pr.getPageSize());
			pager.setData(DTOUtils.convertList(list, PlatformRolePermissionResult.class));
		}
		return pager;
	}
	
	@Override
	public Integer count(PlatformRolePermissionQuery param){
		Integer count=platformRolePermissionDao.count(param);
		return count;
	}
	
	@Override
	public PlatformRolePermissionResult getDetail(Integer id){
		PlatformRolePermission item=platformRolePermissionDao.findById(id);
		if(item==null){
			return null;
		}
		PlatformRolePermissionResult result=new PlatformRolePermissionResult();
		BeanUtilExt.copyProperties(result, item);
		
		return result;
	}

	@Override
	public List<PlatformRolePermissionResult> getList(PlatformRolePermissionQuery param, PagerInfo pr){
		List<PlatformRolePermission> list=platformRolePermissionDao.page(param, pr.getStart(), pr.getPageSize());
		return DTOUtils.convertList(list, PlatformRolePermissionResult.class);
	}
	
	@Override
	public List<PlatformRolePermissionResult> getList(PlatformRolePermissionQuery param){
		List<PlatformRolePermission> list=platformRolePermissionDao.page(param, 0, Integer.MAX_VALUE);
		return DTOUtils.convertList(list, PlatformRolePermissionResult.class);
	}
	
	@Override
	public List<PlatformRolePermissionResult> getList(){
		PlatformRolePermissionQuery param = new PlatformRolePermissionQuery();
		List<PlatformRolePermission> list=platformRolePermissionDao.page(param, 0, Integer.MAX_VALUE);
		return DTOUtils.convertList(list, PlatformRolePermissionResult.class);
	}


	@Override
	public void delete(Integer id) {
		platformRolePermissionDao.deleteById(id);
	}

	@Override
	public void addOrUpdate(PlatformRolePermissionResult result)throws BusinessException {
		PlatformRolePermission item=new PlatformRolePermission();
		BeanUtilExt.copyProperties(item, result);
		if(item.getId()==null) {
			item.setCreateTime(new Date());
			platformRolePermissionDao.insert(item);
		}else {
			PlatformRolePermission tmp=platformRolePermissionDao.findById(item.getId());
			if(tmp==null) {
				throw new BusinessException(ExceptionEnum.ERROR_0002.getCode(), ExceptionEnum.ERROR_0002.getMessage());
			}
			platformRolePermissionDao.updateById(item);
		}
	}
	
	@Override
	public Map<Integer,PlatformRolePermissionResult> getItemMap(Set<Integer> idSet){
		Map<Integer, PlatformRolePermissionResult> map=new HashMap<Integer, PlatformRolePermissionResult>();
		List<PlatformRolePermission> list=platformRolePermissionDao.findByIds(StringUtil.setToList(idSet));
		List<PlatformRolePermissionResult> resultList=DTOUtils.convertList(list, PlatformRolePermissionResult.class);
		for(PlatformRolePermissionResult item:resultList) {
			map.put(item.getId(), item);
		}
		return map;
	}
	
	@Override
	public Map<Integer,PlatformRolePermissionResult> getItemMap(){
		Map<Integer, PlatformRolePermissionResult> map=new HashMap<Integer, PlatformRolePermissionResult>();
		PlatformRolePermissionQuery param=new PlatformRolePermissionQuery();
		List<PlatformRolePermission> list=platformRolePermissionDao.page(param, 0, Integer.MAX_VALUE);
		List<PlatformRolePermissionResult> resultList=DTOUtils.convertList(list, PlatformRolePermissionResult.class);
		for(PlatformRolePermissionResult item:resultList) {
			map.put(item.getId(), item);
		}
		return map;
	}	
	
	
	
}
